import { mapGetters } from 'vuex';
import { getAreaList, toggleAreaInfo } from "@/views/personnel-manage/personnel-manage.service";
import { alertMessage } from "@/utils/utils";

const geoData = [
  {
    'value': '410000',
    'text': '河南省',
    'parentVal': '0',
    children: [
      { 'value': "410100", 'text': "郑州市", "parentVal": "410000" },
      { 'value': "410200", 'text': "开封市", "parentVal": "410000" },
      { 'value': "410300", 'text': "洛阳市", "parentVal": "410000" },
      { 'value': "410400", 'text': "平顶山市", "parentVal": "410000" },
      { 'value': "410500", 'text': "安阳市", "parentVal": "410000" },
      { 'value': "410600", 'text': "鹤壁市", "parentVal": "410000" },
      { 'value': "410700", 'text': "新乡市", "parentVal": "410000" },
      { 'value': "410800", 'text': "焦作市", "parentVal": "410000" },
      { 'value': "410900", 'text': "濮阳市", "parentVal": "410000" },
      { 'value': "411000", 'text': "许昌市", "parentVal": "410000" },
      { 'value': "411100", 'text': "漯河市", "parentVal": "410000" },
      { 'value': "411200", 'text': "三门峡市", "parentVal": "410000" },
      { 'value': "411300", 'text': "南阳市", "parentVal": "410000" },
      { 'value': "411400", 'text': "商丘市", "parentVal": "410000" },
      { 'value': "411500", 'text': "信阳市", "parentVal": "410000" },
      { 'value': "411600", 'text': "周口市", "parentVal": "410000" },
      { 'value': "411700", 'text': "驻马店市", "parentVal": "410000" }
    ]
  }
];
export default {
  name: 'department-management',
  components: {},
  directives: {},
  data() {
    return {
      data: [], // 列表数据
      map: null, // 地图对象
      polygons: [], // 区域边界
      drawInstance: null, // 绘制行政区域实例
      selectedAreaInfo: {
        name: '',
        points: []
      }, // 行政区域边界
      selectedData: {
        value: '410000'
      } // 选中数据详情
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getDataList();
  },
  mounted() {
    this.initMap();
  },
  beforeDestroy() {
    this.map && this.map.destroy();
  },
  methods: {
    // 获取列表
    getDataList() {
      const params = {
        pageNum: 0,
        recordNum: 0
      };
      getAreaList(params).then(res => {
        geoData[0].children.forEach(item => {
          item.isEnable = '0'; // 0否 1是
          for (let i = 0; i < res.data.length; i++) {
            if (item.text == res.data[i].regName) {
              item.objectId = res.data[i].objectId;
              item.isEnable = res.data[i].isEnable;
              break;
            }
          }
        });
        this.data = geoData;
      });
    },

    // 初始化地图
    initMap() {
      this.map = new AMap.Map('map-container', {
        resizeEnable: true // 是否监控地图容器尺寸变化
      });
      this.drawBounds('河南省');
    },

    // 绘制行政区域
    drawBounds(cityName) {
      if (!this.drawInstance) {
        this.drawInstance = new AMap.DistrictSearch({
          subdistrict: 0, // 获取边界不需要返回下级行政区
          extensions: 'all' // 返回行政区边界坐标组等具体信息
        });
      }

      this.drawInstance.search(cityName, (status, result) => {
        this.map.remove(this.polygons); // 清除上次结果
        this.polygons = [];
        this.selectedAreaInfo = {
          name: cityName,
          points: []
        };
        const bounds = result.districtList[0].boundaries;
        if (bounds) {
          this.selectedAreaInfo.points = bounds[0];
          for (let i = 0, l = bounds.length; i < l; i++) {
            // 生成行政区划polygon
            const polygon = new AMap.Polygon({
              strokeWeight: 1,
              path: bounds[i],
              fillOpacity: 0.4,
              fillColor: '#80d8ff',
              strokeColor: '#0091ea'
            });
            this.polygons.push(polygon);
          }
        }
        this.map.add(this.polygons)
        this.map.setFitView(this.polygons); // 视口自适应
      })
    },

    // 设置选中城市的行政区域
    setSelectedArea(rowData) {
      this.selectedData = rowData;
      this.drawBounds(rowData.text);
    },

    // 启用停用
    toggleAreaStatus(rowData, status) {
      this.selectedData = rowData;
      let params = {};
      if (rowData.objectId) {
        params = {
          objectId: rowData.objectId,
          isEnable: status
        };
        this.uploadAreaInfo(params);
        this.drawBounds(rowData.text);
      } else {
        params = {
          isEnable: status,
          regName: rowData.text,
          regCollection: []
        };
        if (rowData.text == this.selectedAreaInfo.name) {
          params.regCollection = this.selectedAreaInfo.points;
          this.uploadAreaInfo(params);
        } else {
          this.drawInstance.search(rowData.text, (status, result) => {
            this.map.remove(this.polygons); // 清除上次结果
            this.polygons = [];
            this.selectedAreaInfo = {
              name: rowData.text,
              points: []
            };
            const bounds = result.districtList[0].boundaries;
            if (bounds) {
              this.selectedAreaInfo.points = bounds[0];
              for (let i = 0, l = bounds.length; i < l; i++) {
                // 生成行政区划polygon
                const polygon = new AMap.Polygon({
                  strokeWeight: 1,
                  path: bounds[i],
                  fillOpacity: 0.4,
                  fillColor: '#80d8ff',
                  strokeColor: '#0091ea'
                });
                this.polygons.push(polygon);
              }
            }
            this.map.add(this.polygons);
            this.map.setFitView(this.polygons); // 视口自适应
            params.regCollection = this.selectedAreaInfo.points;
            this.uploadAreaInfo(params);
          });
        }
      }
    },

    // 上传area状态信息
    uploadAreaInfo(params) {
      toggleAreaInfo(params).then(res => {
        alertMessage(res.message);
        this.getDataList();
      });
    }
  }
}
